import matplotlib.pyplot as plt
import numpy as np
import os
import re

# 实验数据
experiments = [
    {
        "id": "16node-256proc-cn[8473-8474,9544-9545,16920-16922,16985-16987,17027-17029,17386-17388]-20250709_092108",
        "data": [
            (0, 6, 2.03),
            (296, 20002, 1.126789),
            (400, 20002, 1.191784),
            (888, 20001, 1.424949),
            (1016, 10001, 1.248397),
            (1024, 10001, 1.686402),
            (1200, 20001, 2.070435),
            (1360, 20002, 1.501073),
            (1776, 1, 2.16),
            (2400, 1, 10.0),
            (2448, 20002, 1.881637),
            (3048, 10000, 1.890678),
            (3072, 10001, 1.947587),
            (3264, 20002, 1.970981),
            (4080, 20001, 2.321324),
            (6096, 1, 3.52),
            (7344, 20001, 3.858532),
            (8160, 1, 4.78),
            (9792, 20001, 5.235296),
            (14688, 1, 6.5),
            (19584, 1, 13.101),
        ],
        "O": 1.008966,
        "L": 0.000402071
    },
    {
        "id": "16node-256proc-cn[8473-8474,9544-9545,16920-16922,16985-16987,17027-17029,17386-17388]-20250709_092134",
        "data": [
            (0, 6, 2.090167),
            (296, 20002, 1.137848),
            (400, 20002, 1.24652),
            (888, 20001, 1.484141),
            (1016, 10001, 1.22956),
            (1024, 10001, 1.696313),
            (1200, 20001, 1.948761),
            (1360, 20002, 1.487911),
            (1776, 1, 2.74),
            (2400, 1, 9.26),
            (2448, 20002, 1.883853),
            (3048, 10000, 1.881296),
            (3072, 10001, 2.052454),
            (3264, 20002, 1.946427),
            (4080, 20001, 2.326297),
            (6096, 1, 2.8),
            (7344, 20001, 3.746858),
            (8160, 1, 4.08),
            (9792, 20001, 5.19131),
            (14688, 1, 5.02),
            (19584, 1, 13.1),
        ],
        "O": 1.018821,
        "L": 0.000393013
    },
    {
        "id": "16node-256proc-cn[8473-8474,9544-9545,16920-16922,16985-16987,17027-17029,17386-17388]-20250709_092159",
        "data": [
            (0, 6, 2.24),
            (296, 20002, 1.102677),
            (400, 20002, 1.235685),
            (888, 20001, 1.492555),
            (1016, 10001, 1.291346),
            (1024, 10001, 1.744603),
            (1200, 20001, 2.184149),
            (1360, 20002, 1.494045),
            (1776, 1, 2.9),
            (2400, 1, 9.28),
            (2448, 20002, 1.952859),
            (3048, 10000, 1.93842),
            (3072, 10001, 2.023213),
            (3264, 20002, 1.937088),
            (4080, 20001, 2.340709),
            (6096, 1, 3.0),
            (7344, 20001, 3.890967),
            (8160, 1, 11.24),
            (9792, 20001, 5.463557),
            (14688, 1, 18.961),
            (19584, 1, 11.88),
        ],
        "O": 1.018263,
        "L": 0.00041554
    },
    {
        "id": "16node-256proc-cn[8473-8474,9544-9545,16920-16922,16985-16987,17027-17029,17386-17388]-20250709_092227",
        "data": [
            (0, 6, 2.033333),
            (296, 20002, 1.212886),
            (400, 20002, 1.334127),
            (888, 20001, 1.49241),
            (1016, 10001, 1.391051),
            (1024, 10001, 1.701977),
            (1200, 20001, 2.119328),
            (1360, 20002, 1.537621),
            (1776, 1, 2.5),
            (2400, 1, 9.6),
            (2448, 20002, 1.875504),
            (3048, 10000, 1.918631),
            (3072, 10001, 2.1554),
            (3264, 20002, 2.02252),
            (4080, 20001, 2.395515),
            (6096, 1, 3.04),
            (7344, 20001, 3.80313),
            (8160, 1, 3.72),
            (9792, 20001, 5.15892),
            (14688, 1, 8.56),
            (19584, 1, 10.86),
        ],
        "O": 1.100416,
        "L": 0.00038452
    },
    {
        "id": "16node-256proc-cn[8473-8474,9544-9545,16920-16922,16985-16987,17027-17029,17386-17388]-20250709_092252",
        "data": [
            (0, 6, 2.203333),
            (296, 20002, 1.205239),
            (400, 20002, 1.285483),
            (888, 20001, 1.440211),
            (1016, 10001, 1.344055),
            (1024, 10001, 1.700031),
            (1200, 20001, 2.059476),
            (1360, 20002, 1.526151),
            (1776, 1, 2.3),
            (2400, 1, 7.14),
            (2448, 20002, 1.905519),
            (3048, 10000, 1.858659),
            (3072, 10001, 2.078927),
            (3264, 20002, 1.948616),
            (4080, 20001, 2.343605),
            (6096, 1, 3.62),
            (7344, 20001, 3.836763),
            (8160, 1, 10.04),
            (9792, 20001, 5.310706),
            (14688, 1, 14.42),
            (19584, 1, 10.36),
        ],
        "O": 1.040852,
        "L": 0.000400787
    },
    {
        "id": "16node-256proc-cn[8473-8474,9544-9545,16920-16922,16985-16987,17027-17029,17386-17388]-20250709_092317",
        "data": [
            (0, 6, 2.110167),
            (296, 20002, 1.189209),
            (400, 20002, 1.279093),
            (888, 20001, 1.479326),
            (1016, 10001, 1.33987),
            (1024, 10001, 1.644426),
            (1200, 20001, 2.069793),
            (1360, 20002, 1.484362),
            (1776, 1, 3.18),
            (2400, 1, 10.92),
            (2448, 20002, 1.855177),
            (3048, 10000, 1.864741),
            (3072, 10001, 1.987761),
            (3264, 20002, 1.927893),
            (4080, 20001, 2.263377),
            (6096, 1, 2.74),
            (7344, 20001, 3.720235),
            (8160, 1, 3.64),
            (9792, 20001, 5.332453),
            (14688, 1, 8.1),
            (19584, 1, 8.82),
        ],
        "O": 1.027332,
        "L": 0.000396743
    },
    {
        "id": "16node-256proc-cn[8473-8474,9544-9545,16920-16922,16985-16987,17027-17029,17386-17388]-20250709_092342",
        "data": [
            (0, 6, 8.173333),
            (296, 20002, 1.216346),
            (400, 20002, 1.291727),
            (888, 20001, 1.533443),
            (1016, 10001, 1.358081),
            (1024, 10001, 1.718145),
            (1200, 20001, 2.124424),
            (1360, 20002, 1.545672),
            (1776, 1, 3.26),
            (2400, 1, 6.98),
            (2448, 20002, 1.881817),
            (3048, 10000, 1.857044),
            (3072, 10001, 2.083354),
            (3264, 20002, 1.981339),
            (4080, 20001, 2.670178),
            (6096, 1, 3.9),
            (7344, 20001, 3.924913),
            (8160, 1, 11.82),
            (9792, 20001, 5.268446),
            (14688, 1, 13.54),
            (19584, 1, 10.9),
        ],
        "O": 1.097944,
        "L": 0.000400968
    },
    {
        "id": "16node-256proc-cn[8473-8474,9544-9545,16920-16922,16985-16987,17027-17029,17386-17388]-20250709_092408",
        "data": [
            (0, 6, 2.740167),
            (296, 20002, 1.113297),
            (400, 20002, 1.204869),
            (888, 20001, 1.500055),
            (1016, 10001, 1.233786),
            (1024, 10001, 1.567733),
            (1200, 20001, 2.195126),
            (1360, 20002, 1.439226),
            (1776, 1, 2.76),
            (2400, 1, 7.3),
            (2448, 20002, 1.737956),
            (3048, 10000, 1.884847),
            (3072, 10001, 1.93579),
            (3264, 20002, 1.862589),
            (4080, 20001, 2.182905),
            (6096, 1, 3.26),
            (7344, 20001, 3.719431),
            (8160, 1, 3.76),
            (9792, 20001, 4.954805),
            (14688, 1, 8.6),
            (19584, 1, 11.26),
        ],
        "O": 1.037955,
        "L": 0.000371097
    },
    {
        "id": "16node-256proc-cn[8473-8474,9544-9545,16920-16922,16985-16987,17027-17029,17386-17388]-20250709_092434",
        "data": [
            (0, 6, 6.896667),
            (296, 20002, 1.147992),
            (400, 20002, 1.247456),
            (888, 20001, 1.482943),
            (1016, 10001, 1.225786),
            (1024, 10001, 1.666853),
            (1200, 20001, 2.208624),
            (1360, 20002, 1.514843),
            (1776, 1, 2.14),
            (2400, 1, 6.82),
            (2448, 20002, 1.919371),
            (3048, 10000, 1.874979),
            (3072, 10001, 1.976965),
            (3264, 20002, 1.966615),
            (4080, 20001, 2.32112),
            (6096, 1, 3.54),
            (7344, 20001, 3.753802),
            (8160, 1, 7.36),
            (9792, 20001, 5.051766),
            (14688, 1, 10.1),
            (19584, 1, 10.78),
        ],
        "O": 1.091554,
        "L": 0.000376513
    },
    {
        "id": "16node-256proc-cn[8473-8474,9544-9545,16920-16922,16985-16987,17027-17029,17386-17388]-20250709_092500",
        "data": [
            (0, 6, 2.84),
            (296, 20002, 1.200559),
            (400, 20002, 1.323074),
            (888, 20001, 1.425637),
            (1016, 10001, 1.304713),
            (1024, 10001, 1.694475),
            (1200, 20001, 2.084763),
            (1360, 20002, 1.53784),
            (1776, 1, 2.96),
            (2400, 1, 9.18),
            (2448, 20002, 1.967344),
            (3048, 10000, 1.876327),
            (3072, 10001, 2.114239),
            (3264, 20002, 1.964228),
            (4080, 20001, 2.35974),
            (6096, 1, 2.66),
            (7344, 20001, 3.821464),
            (8160, 1, 9.24),
            (9792, 20001, 5.461381),
            (14688, 1, 17.68),
            (19584, 1, 10.941),
        ],
        "O": 1.042103,
        "L": 0.000409533
    }
]
# 设置输出目录
base_dir = "F:\PostGraduate\Point-to-Point-DATA\deal-data-code\C-lop-Prediction\lammps_and_the_line_of_clop_for_count_bigger_than10_data"
csv_dir = "16node/"
output_dir = os.path.join(base_dir, csv_dir)
os.makedirs(output_dir, exist_ok=True)

def plot_experiment(exp):
    """绘制单个实验的图表"""
    # 准备数据 - 过滤掉count<10的数据点
    sizes = []
    times = []
    for size, count, time in exp["data"]:
        if count >= 10:
            sizes.append(size)
            times.append(time)
    
    # 准备线性模型数据
    min_size = 0
    max_size = max(sizes)
    model_sizes = np.linspace(min_size, max_size, 50)
    model_times = exp["O"] + exp["L"] * model_sizes
    
    plt.figure(figsize=(10, 6))
    
    # 绘制实际数据点 (曲线)
    plt.scatter(sizes, times, color='blue', s=50, label='real data')
    
    # 绘制线性模型 (直线)
    plt.plot(model_sizes, model_times, 'r-', linewidth=2, label=f'clop model: t = {exp["O"]:.2f} + {exp["L"]:.6f} * size')
    
    plt.title(f'Lammps_cu and the Clop result- {exp["id"]}', fontsize=14)
    plt.xlabel('comm_size (Bytes)', fontsize=12)
    plt.ylabel('comm_time (μs)', fontsize=12)
    plt.grid(True, linestyle='--', alpha=0.7)
    plt.legend()
    
    # 添加延迟和带宽信息
    # plt.text(0.05, 0.85, f' O = {exp["O"]:.2f} μs\n  L = {exp["L"]*1000:.3f} ns/Byte',
    #          transform=plt.gca().transAxes, bbox=dict(facecolor='white', alpha=0.8))
    
    # 优化布局
    plt.tight_layout()
    
    # 保存图表
    filename = exp["id"].replace("[", "").replace("]", "").replace(":", "_") + ".png"
    plt.savefig(os.path.join(output_dir, filename))
    plt.close()
    print(f"已保存图表: {filename}")

# 为每个实验创建图表
for exp in experiments:
    plot_experiment(exp)

print(f"所有图表已保存至: {output_dir}")